package code.tools.commponents
{
	import code.structure.manager.loader.DllLoaderManager;
	import code.structure.manager.font.FontManager;
	import code.structure.manager.language.LanguageManager;
	import code.untils.UiNameConst;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	/**
	 *+ - 组件 
	 * @author liangqian
	 * 
	 */
	public class NumberStep extends Component
	{
		
		private var pageTurning:PageTurning;
		/**
		 *输入数量框
		 */
		private var inputNumberTf:TextField;

		private var onChange:Function;

		public function NumberStep(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, handler:Function=null)
		{
			setSize(138, 34);
			super(parent, xpos, ypos);
			onChange=handler;
			setValue(1);
		}

		override protected function addChildren():void
		{
			super.addChildren();
			var borderBg:Sprite = new(DllLoaderManager.getInstance().getMaterial(UiNameConst.ALERT_VIEW,'NumberStep_bg') as Class) as Sprite;
			addChild(borderBg);
			borderBg.x = 30;
			/* 输入数量框 */
			var tf:TextFormat = new TextFormat();
			tf.size = 23;
			tf.font = FontManager.getInstance().embeddedFontBName;
			inputNumberTf=Helpers.FormatTextField(this, 32, 0,72,TextFieldAutoSize.CENTER,0x666666);
			inputNumberTf.restrict='0-9';
			inputNumberTf.type = TextFieldType.INPUT;
			inputNumberTf.defaultTextFormat = tf;
			inputNumberTf.addEventListener(Event.CHANGE, inputNumberHandler);
			
			pageTurning = new PageTurning(this,0,1,160,pageTurningHandler);
			pageTurning.scaleX = .58;
			pageTurning.scaleY = .61;
		}

		private var _value:int;
		private var _maximum:int;
		private var _minimum:int;

		override public function draw():void
		{
			super.draw();
			inputNumberTf.text=_value + '';
		}
		
		private function pageTurningHandler(...args):void
		{
			value = pageTurning.value;
			onChange();
		}

		public function setValue(v:int, min:int=1, max:int=999):void
		{
			if (_value == v && _maximum == max && _minimum == min)
				return;
			_value=v;
			_maximum=max;
			_minimum=min;
			pageTurning.value = _value;
			pageTurning.max = _maximum;
			pageTurning.min = _minimum;
			correctValue();
			invalidate();
		}

		public function get value():int
		{
			return _value;
		}

		public function set value(value:int):void
		{
			if (_value == value)
				return;
			_value=value;
			correctValue();
			invalidate();
		}

		private function correctValue():void
		{
			if (_maximum < _minimum)
			{
				var t:int=_maximum;
				_maximum=_minimum;
				_minimum=t;
			}
			_value=Math.max(_minimum, Math.min(_value, _maximum));
		}

		/**
		 *	输入数字时
		 */
		private function inputNumberHandler(e:Event):void
		{
			e.stopImmediatePropagation();
			var newVal:int=int(inputNumberTf.text);
			value=newVal;
			pageTurning.value = value;
			onChange();
		}
	}
}